package com.hc.controller;

import com.hc.common.bean.PageBean;
import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.domain.Order;
import com.hc.res.OrderRes;
import com.hc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author hecai
 * @date 2021/12/22 15:07
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping
    public Result create(@RequestBody Order order) {
        boolean res = orderService.save(order);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result updateById(@RequestBody Order order) {
        boolean res = orderService.updateById(order);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result removeById(@PathVariable Serializable id) {
        boolean res = orderService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Serializable id) {
        Order res = orderService.getById(id);
        return ResultUtil.success("查询成功").setData(res);
    }

    //@PostMapping
    //public Result getByCondition(@RequestBody OrderQuery condition) {
    //    List<Order> list = orderService.queryByCondition(condition);
    //    return ResultUtil.success("查询成功").setData(list);
    //}

    /**
     * 去结算--提交订单
     *
     * @param addId   收件地址编号
     * @param payType 支付方式
     * @param skuIds  要购买的商品编号
     * @return
     */
    @PostMapping("/buy")
    public Result buy(Long addId, Integer payType, List<Integer> skuIds) {
        boolean res = orderService.buy(addId, payType, skuIds);
        if (res) {
            return ResultUtil.success();
        } else {
            return ResultUtil.fail();
        }
    }

    @GetMapping("/myOrder/{userId}/{status}/{pageNum}/{pageSize}")
    public Result myOrder(@PathVariable("userId") Integer userId,
                          @PathVariable("status") Integer status,
                          @PathVariable("pageNum") Integer pageNum,
                          @PathVariable("pageSize") Integer pageSize) {
        PageBean<OrderRes> pageBean = orderService.getOrderByUserId(userId, status, pageNum, pageSize);

        return ResultUtil.success().setData(pageBean);
    }


}